import requests
import json


# OKX API的公共URL
base_url = "https://www.okx.com"

# 获取资金费率
def get_funding_rate(symbol):
    endpoint = "/api/v5/public/funding-rate"
    params = {
        'instId': symbol
    }
    response = requests.get(base_url + endpoint, params=params)
    data = response.json()
    return data

def get_funding_rate_history(symbol):
    endpoint = "/api/v5/public/funding-rate-history"
    params = {
        'instId': symbol
    }
    response = requests.get(base_url + endpoint, params=params)
    data = response.json()
    return data

# 获取订单簿
def get_order_book(symbol, depth=50):
    endpoint = "/api/v5/market/books-grouped"
    params = {
        'instId': symbol,
        
        'grouping':100
    }
    response = requests.get(base_url + endpoint, params=params)
    data = response.json()
    return data

# 示例：获取BTC-USDT资金费率
symbol = "BTC-USDT-SWAP"  # 合约名称，例如BTC-USDT永续合约
# funding_rate = get_funding_rate(symbol)
# print("Funding Rate:", json.dumps(funding_rate, indent=4))

# # 示例：获取BTC-USDT的订单簿
# order_book = get_order_book(symbol=symbol)
# print("Order Book:", json.dumps(order_book, indent=4))

funding_rate_history = get_funding_rate_history(symbol)

print("Order Book:", json.dumps(funding_rate_history, indent=4))